package cn.sh.library.mgts.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchAction;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import cn.sh.library.mgts.bean.DataSyncLog;
import cn.sh.library.mgts.common.Constant;
import cn.sh.library.mgts.service.ConfigService;
import cn.sh.library.mgts.service.IndexService;

//索引库获取数据
@Service
public class ConfigServiceImpl implements ConfigService {

	@Autowired
	RestHighLevelClient highLevelClient;
	
	@Autowired
	IndexService indexService;

	private static Logger logger = LoggerFactory.getLogger(ConfigServiceImpl.class);
	@Override
	public List<Map<String, Object>> getUserList() {
		List<Map<String, Object>> res = new ArrayList<>();
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.size(9999);

		SearchRequest searchRequest = new SearchRequest(Constant.BIB_HORIZON_USER);
		searchRequest.source(searchSourceBuilder);
		try {
			SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			searchResponse.getHits().forEach(action -> {
				res.add((Map<String, Object>) action.getSourceAsMap());
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public List<Map<String, String>> getConfigList() {
		List<Map<String, String>> res = new ArrayList<>();

		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.from(0);
		searchSourceBuilder.size(9999);
		SearchRequest searchRequest = new SearchRequest(Constant.BIB_RELATION_INDEX_NAME);
		searchRequest.source(searchSourceBuilder);
		try {
			SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			searchResponse.getHits().forEach(action -> {
//				System.out.println(action);
//				action.getSourceAsString();
//				System.out.println(action.getSourceAsMap().get("relation"));
				res.add((Map<String, String>) action.getSourceAsMap().get("relation"));
			});
//			searchResponse.getHits()
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	

	@Override
	public List<Map<String, Object>> getLocationList(String keyword) {
		List<Map<String, Object>> res = new ArrayList<>();

		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		if(StringUtils.isNotBlank(keyword)) {
			searchSourceBuilder.query(QueryBuilders.boolQuery().should(QueryBuilders.matchPhraseQuery("location.location", keyword)).should(QueryBuilders.matchPhraseQuery("location.location_name", keyword)));
		}
		searchSourceBuilder.size(9999);

		SearchRequest searchRequest = new SearchRequest(Constant.ITEM_LOCATION_INDEX_NAME);
		searchRequest.source(searchSourceBuilder);
		try {
			SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			searchResponse.getHits().forEach(action -> {
				res.add((Map<String, Object>)action.getSourceAsMap().get("location"));
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public List<Map<String, String>> getLocationList() {
		List<Map<String, String>> res = new ArrayList<>();

		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.size(9999);

		SearchRequest searchRequest = new SearchRequest(Constant.ITEM_LOCATION_INDEX_NAME);
		searchRequest.source(searchSourceBuilder);
		try {
			SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			searchResponse.getHits().forEach(action -> {
				res.add((Map<String, String>) action.getSourceAsMap().get("location"));
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public List<Map<String, String>> getCollectionList() {
		List<Map<String, String>> res = new ArrayList<>();
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.size(9999);

		SearchRequest searchRequest = new SearchRequest(Constant.ITEM_COLLECTION_INDEX_NAME);
		searchRequest.source(searchSourceBuilder);
		try {
			SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			searchResponse.getHits().forEach(action -> {
				res.add((Map<String, String>) action.getSourceAsMap().get("collection"));
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	@Override
	public List<Map<String, Object>> getCollectionList(String keyword) {
		List<Map<String, Object>> res = new ArrayList<>();
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.size(9999);

		if(StringUtils.isNotBlank(keyword)) {
			searchSourceBuilder.query(QueryBuilders.boolQuery().should(QueryBuilders.matchPhraseQuery("collection.collection", keyword)).should(QueryBuilders.matchPhraseQuery("collection.explain", keyword)));
		}
		SearchRequest searchRequest = new SearchRequest(Constant.ITEM_COLLECTION_INDEX_NAME);
		searchRequest.source(searchSourceBuilder);
		try {
			SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			searchResponse.getHits().forEach(action -> {
				res.add((Map<String, Object>)action.getSourceAsMap().get("collection"));
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	@Override
	public Map<String, Object> place(){
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.size(9999);
		QueryBuilder q = QueryBuilders.matchAllQuery();
		searchSourceBuilder.query(q);
		SearchRequest searchRequest = new SearchRequest(Constant.PLACE_INDEX);
		searchRequest.source(searchSourceBuilder);
		Map<String, Object> res = new HashMap<>();
		try {
			SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			searchResponse.getHits().forEach(action -> {
//				res.add((Map<String, String>) action.getSourceAsMap());
				res.put(action.getId(), action.getSourceAsMap());
			});
		} catch (IOException e) {
			e.printStackTrace();
		}

		return  res;
	}

	@Override
	public List<Map<String, String>> getItypeList() {
		List<Map<String, String>> res = new ArrayList<>();
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.size(9999);

		SearchRequest searchRequest = new SearchRequest(Constant.ITEM_ITYPE_INDEX_NAME);
		searchRequest.source(searchSourceBuilder);
		try {
			SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
			searchResponse.getHits().forEach(action -> {
				res.add((Map<String, String>) action.getSourceAsMap().get("itype"));
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
}
